import ErrorCode from "../script/ErrorCode";
import { Toast } from "vant";
export const forget = {
  created() {},
  methods: {
    initialize() {
      this.send.phoneNumber = "";
      this.send.phoneAuthCode = "";
      this.send.password = "";
      this.notarize='';
    },
    //提示语
    hint(status, txt) {
      this[status] = txt;
      const self = this;
      setTimeout(() => {
        self[status] = "";
      }, 3000);
    },
    submit() {
      const self = this;
      if (!this.$validate.phone.regex.test(this.send.phoneNumber)) {
        return this.hint("errors", this.$validate.phone.error);
      }
      if (!this.$validate.notEmpty.regex.test(this.send.phoneAuthCode)) {
        return this.hint("errors", this.$validate.notEmpty.error);
      }
      if (!this.$validate.newpassword.regex.test(this.send.password)) {
        return this.hint("errors", this.$validate.newpassword.error);
      }
      // 确认密码
      if (!this.$validate.newpassword.regex.test(this.notarize)) {
        return this.hint("errors", this.$validate.newpassword.error);
      }
      if(this.send.password != this.notarize){
        return this.hint("errors", '两次填写的密码不一致');
     }
      if (!navigator.onLine) return Toast("网络连接失败，请检查网络设置");
      this.$store
        .dispatch("resetPassword", {
          phoneNumber: this.$fn.encryptByDES(this.send.phoneNumber),
          phoneAuthCode: this.send.phoneAuthCode,
          password: this.$fn.encryptByDES(this.send.password),
        })
        .then(function(data) {
          if (data.status == 1) {
            self.gologin = true;
            self.rockon();
          } else {
            self.showError(ErrorCode[data.status]);
          }
        });
    },
    showError(val) {
      this.hint("errors", val);
    },
    confirm(authCode) {
      const self = this;
      if (!navigator.onLine) return Toast("网络连接失败，请检查网络设置");
      this.$store
        .dispatch("sendPhoneAuthcode", {
          phoneNumber: this.$fn.encryptByDES(this.send.phoneNumber),
          imageAuthcode: authCode,
        })
        .then(function(data) {
          if (data.status != 1) {
            return self.showError(ErrorCode[data.status]);
          }
          self.countDown();
          self.hint("success", "验证码已发送至" + self.send.phoneNumber);
        });
    },
    rockon() {
      const self = this,
        timer = setInterval(() => {
          if (self.reciprocal == 1) {
            clearInterval(timer);
            return self.toggleType();
          }
          self.reciprocal--;
        }, 1000);
    },
    toggleType(num) {
      //调用登录窗口
      window.prompt(
        "",
        `up://["web_user_login",${Date.now()},{"type":"client_login","data":{}}]`
      );
      // this.$emit("toggleType", num);
    },
    getAuthCode() {
      if (!this.$validate.notEmpty.regex.test(this.send.phoneNumber)) {
        return this.hint("errors", this.$validate.notEmpty.error);
      }
      if (!this.$validate.phone.regex.test(this.send.phoneNumber)) {
        return this.hint("errors", this.$validate.phone.error);
      }
      this.authShow = !this.authStatus;
    },
    countDown() {
      const TIME_COUNT = 59,
        self = this;
      let timer;
      if (!timer) {
        let second = TIME_COUNT;
        this.authStatus = 1;
        self.count = "60s重新获取";
        timer = setInterval(() => {
          if (second > 0 && second <= TIME_COUNT) {
            self.count = second-- + "s重新获取";
          } else {
            clearInterval(timer);
            timer = null;
            self.authStatus = 0;
          }
        }, 1000);
      }
    },
  },
};
